package com.itsci.assignment1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ShowPersonalHistory extends Activity{
	private TextView name;
	private TextView address;
	private TextView phone;
	private TextView birthday;
	private TextView height;
	private TextView age ;
	private TextView weight;
	private TextView appear;
	private TextView be;
	private Button btnEdit;
	private String[] text;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history_personal);
		name = (TextView)findViewById(R.id.textView_his_Per_name);
		address = (TextView)findViewById(R.id.textView_his_Per_address);
		phone = (TextView)findViewById(R.id.textView_his_Per_phone);
		birthday = (TextView)findViewById(R.id.textView_his_Per_birthday);
		height = (TextView)findViewById(R.id.textView_his_Per_height);
		age = (TextView)findViewById(R.id.textView_his_Per_age);
		weight = (TextView)findViewById(R.id.textView_his_Per_weight);
		appear = (TextView)findViewById(R.id.textView_his_Per_appear);
		be = (TextView)findViewById(R.id.textView_his_Per_Be);
		btnEdit = (Button)findViewById(R.id.button_his_Per_Edit);
		Intent intent= getIntent();
		String getName = intent.getStringExtra("name");
		text = ReadFile(getName);
		name.setText(text[0]);
		birthday.setText(text[1]);
		age.setText(text[2]);
		height.setText(text[3]);
		weight.setText(text[4]);
		address.setText(text[5]);
		phone.setText(text[6]);
		appear.setText(text[7]);
		be.setText(text[8]);
		
		btnEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent gotoEdit = new Intent(ShowPersonalHistory.this,EditPersonHistory.class);
				gotoEdit.putExtra("name",text[0]);
				gotoEdit.putExtra("birthday",text[1]);
				gotoEdit.putExtra("age",text[2]);
				gotoEdit.putExtra("height",text[3]);
				gotoEdit.putExtra("weight",text[4]);
				gotoEdit.putExtra("address",text[5]);
				gotoEdit.putExtra("phone",text[6]);
				gotoEdit.putExtra("appear",text[7]);
				gotoEdit.putExtra("be",text[8]);
				startActivityForResult(gotoEdit, 111);
//				startActivity(gotoEdit);
			}
		});
		
	}
	public String[] ReadFile(String name) {
		String path = "/mnt/sdcard/TextFile/Patient.txt";
		String[] temp = null;
		try {
			File myFile = new File(path);
			FileInputStream fIn = new FileInputStream(myFile);
			BufferedReader myReader = new BufferedReader(new InputStreamReader(
					fIn));
			String aDataRow = "";
			while ((aDataRow = myReader.readLine()) != null) {
				String text[] = aDataRow.split(",");
				if (name.equalsIgnoreCase(text[0])) {
					temp = aDataRow.split(",");
				}
			}
			myReader.close();

		} catch (Exception e) {
			Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT)
					.show();
		}
		return (String[]) temp;
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Toast.makeText(getBaseContext(), Integer.toString(resultCode), Toast.LENGTH_SHORT).show();
		
		if(resultCode ==111){
		
			name.setText(data.getExtras().getString("name"));
			birthday.setText(data.getStringExtra("birthday"));
			age.setText(data.getStringExtra("age"));
			height.setText(data.getExtras().getString("height"));
			weight.setText(data.getStringExtra("weight"));
			address.setText(data.getStringExtra("address"));
			phone.setText(data.getStringExtra("phone"));
			appear.setText(data.getStringExtra("appear"));
			be.setText(data.getStringExtra("be"));
		}
	}

}
